package com.pc.scrs.service.impl;

import com.pc.scrs.bean.po.Users;
import com.pc.scrs.bean.vo.UsersVo;
import com.pc.scrs.dao.UsersDao;
import com.pc.scrs.service.IUsersService;
import com.pc.scrs.utils.JwtUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional

public class UsersService implements IUsersService {

    @Resource
    private UsersDao usersDao;


    @Override
    public UsersVo userInfo(String token) {
        //根据controller层获取token并解析出userid
        String username = JwtUtils.getUserNameFormJwt(token);
        Users users = usersDao.userInfo(username);
        UsersVo usersVo = new UsersVo();
        usersVo.setId(users.getId());
        usersVo.setUsername(users.getUsername());
        usersVo.setPassword(users.getPassword());
        usersVo.setStatus(users.getStatus());
        usersVo.setSign(users.getSign());
        usersVo.setMobile(users.getMobile());
        usersVo.setAvatar(users.getAvatar());
        usersVo.setName(users.getName());
        return usersVo;
    }

    @Override
    public Integer updatePassword(String token, String password) {
        //根据controller层获取token并解析出userid
        String username = JwtUtils.getUserNameFormJwt(token);
        int result = usersDao.updatePassword(username, new BCryptPasswordEncoder().encode(password));
        return result;
    }

    @Override
    public Integer updateInfo(String token, String name, String sign) {
        //根据controller层获取token并解析出userid
        String username = JwtUtils.getUserNameFormJwt(token);
        int result = usersDao.updateInfo(username, name, sign);
        return result;
    }


}
